
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <cassert>
#include <array>
#include <cmath>
// ./debug.out ../run/checkoutCpp ../../../cfd/fortran/structured_2d/run/check 1
using namespace std;
// constexpr double tol{1.0e-10};
struct Data
{
    int i, j;
    array<double, 6> data{};
};

int main(int argc, char **argv)
{
    if (argc < 5)
    {
        cout << "***.out  file1   file2  int  tol\n"
             << endl;
        exit(EXIT_FAILURE);
    }
    string fname1{argv[1]}, fname2{argv[2]}, str{argv[3]}, str2{argv[4]};
    const double tol = stod(str2);
    int numVar = stoi(str);
    assert(numVar <= 6);
    ifstream file1{fname1}, file2{fname2};
    if (!file1)
    {
        cout << "open file1 error" << endl;
        exit(EXIT_FAILURE);
    }
    if (!file2)
    {
        cout << "open file2 error" << endl;
        exit(EXIT_FAILURE);
    }

    int ibeg1, iend1, jbeg1, jend1;
    int ibeg2, iend2, jbeg2, jend2;
    int numI1, numJ1;
    int numI2, numJ2;

    file1 >> ibeg1 >> iend1 >> jbeg1 >> jend1;
    file2 >> ibeg2 >> iend2 >> jbeg2 >> jend2;

    numI1 = iend1 - ibeg1 + 1;
    numJ1 = jend1 - jbeg1 + 1;
    numI2 = iend2 - ibeg2 + 1;
    numJ2 = jend2 - jbeg2 + 1;
    assert(numI1 == numI2 && numJ1 == numJ2);

    vector<Data> data1;
    vector<Data> data2;
    data1.resize(numI1 * numJ1);
    data2.resize(numI2 * numJ2);

    for (int j = 0; j < numJ1; ++j)
    {
        for (int i = 0; i < numI1; ++i)
        {
            file1 >> data1[j * numI1 + i].i >> data1[j * numI1 + i].j;
            for (int m = 0; m < numVar; ++m)
            {
                file1 >> data1[j * numI1 + i].data[m];
            }
        }
    }
    for (int j = 0; j < numJ2; ++j)
    {
        for (int i = 0; i < numI2; ++i)
        {
            file2 >> data2[j * numI2 + i].i >> data2[j * numI2 + i].j;
            for (int m = 0; m < numVar; ++m)
            {
                file2 >> data2[j * numI2 + i].data[m];
            }
        }
    }

    for (int j = 0; j < numJ2; ++j)
    {
        for (int i = 0; i < numI2; ++i)
        {
            for (int m = 0; m < numVar; ++m)
            {
                if (fabs(data1[j * numI2 + i].data[m] - data2[j * numI2 + i].data[m]) > tol)
                {
                    cout << i + ibeg1 << "    " << j + jbeg1 << "    " << m << "\n";
                }
            }
        }
    }
    file1.close();
    file2.close();
    return 0;
}